/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.data;

import android.database.Cursor;

import com.android.gallery3d.util.GalleryUtils;

import java.text.DateFormat;
import java.util.Date;

//
// LocalMediaItem is an abstract class captures those common fields
// in LocalImage and LocalVideo.
//
public abstract class LocalMediaItem extends MediaItem {

	private static final String TAG = LocalMediaItem.class.getSimpleName();

	// database fields
	public int id;
	public String caption;
	public String mimeType;
	public long fileSize;
	public double latitude = INVALID_LATLNG;
	public double longitude = INVALID_LATLNG;
	public long dateTakenInMs;
	public long dateAddedInSec;
	public long dateModifiedInSec;
	public String filePath;
	public int bucketId;
	public int width;
	public int height;

	public LocalMediaItem(Path path, long version) {
		super(path, version);
	}

	@Override
	public long getDateInMs() {
		return dateTakenInMs;
	}

	@Override
	public String getName() {
		return caption;
	}

	@Override
	public void getLatLong(double[] latLong) {
		latLong[0] = latitude;
		latLong[1] = longitude;
	}

	abstract protected boolean updateFromCursor(Cursor cursor);

	public int getBucketId() {
		return bucketId;
	}

	public void updateContent(Cursor cursor) {
		if (updateFromCursor(cursor)) {
			mDataVersion = nextVersionNumber();
		}
	}

	@Override
	public MediaDetails getDetails() {
		Log.d(TAG, "getDetails");
		MediaDetails details = super.getDetails();
		details.addDetail(MediaDetails.INDEX_PATH, filePath);
		details.addDetail(MediaDetails.INDEX_TITLE, caption);
		DateFormat formater = DateFormat.getDateTimeInstance();
		details.addDetail(MediaDetails.INDEX_DATETIME,
				formater.format(new Date(dateModifiedInSec * 1000)));
		details.addDetail(MediaDetails.INDEX_WIDTH, width);
		details.addDetail(MediaDetails.INDEX_HEIGHT, height);

		if (GalleryUtils.isValidLocation(latitude, longitude)) {
			details.addDetail(MediaDetails.INDEX_LOCATION, new double[] {
					latitude, longitude });
		}
		if (fileSize > 0)
			details.addDetail(MediaDetails.INDEX_SIZE, fileSize);
		return details;
	}

	@Override
	public String getMimeType() {
		return mimeType;
	}

	@Override
	public long getSize() {
		return fileSize;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("LocalMediaItem[id: ").append(id);
		// sb.append(" , caption: ").append(caption);
		// sb.append(" , mineType: ").append(mimeType);
		// sb.append(" , fileSize: ").append(fileSize);
		// sb.append(" , latitude: ").append(latitude);
		// sb.append(" , longitude: ").append(longitude);
		// sb.append(" , dateTakenInMs: ").append(dateTakenInMs);
		// sb.append(" , dateAddedInSec: ").append(dateAddedInSec);
		sb.append(" , date: ")
				.append(new Date(dateAddedInSec).toLocaleString());
		// sb.append(" , dateModifiedInSec: ").append(dateModifiedInSec);
		sb.append(" , filePath: ").append(filePath);
		sb.append(" , bucketId: ").append(bucketId);
		// sb.append(" , width: ").append(width);
		// sb.append(" , height: ").append(height);
		sb.append(" ]");
		return sb.toString();
	}
}
